﻿using System.Speech.Recognition;
using System.Speech.Synthesis;

namespace OffLineVoice
{
    /// <summary>
    /// 语音识别及播报管理者
    /// </summary>
    public class SpeechRecognitionManager : IDisposable
    {
        /// <summary>
        /// 语音播报引擎
        /// </summary>
        private SpeechSynthesizer? speaker;

        /// <summary>
        /// 当前播放的语音
        /// </summary>
        private Prompt? curPrompt;

        /// <summary>
        /// 语音识别引擎
        /// </summary>
        private SpeechRecognitionEngine? recognizer;

        public event Action<string> SpeechRecognizedEventHandler;

        public event Action<string> SpeechRecognitionRejectedEventHandler;

        /// <summary>
        /// 开启语音识别
        /// </summary>
        /// <returns></returns>
        public void StartVoiceRecognition(string[] keywords)
        {
            EndVoiceRecognition();
            recognizer = new SpeechRecognitionEngine();

            // 设置音频输入为默认麦克风
            recognizer.SetInputToDefaultAudioDevice();
            recognizer.SpeechRecognized += Recognizer_SpeechRecognized;
            recognizer.SpeechRecognitionRejected += Recognizer_SpeechRecognitionRejected;

            // 创建一个语音识别 grammer
            Choices choices = new Choices();
            //choices.Add("您好", "开始输入");
            choices.Add(keywords);
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(choices);
            Grammar grammar = new Grammar(gb);
            // 识别事件处理
            recognizer.LoadGrammar(grammar);
            // 开始异步识别
            recognizer.RecognizeAsync(RecognizeMode.Multiple);
        }

        /// <summary>
        /// 结束语音识别
        /// </summary>
        /// <returns></returns>
        public void EndVoiceRecognition()
        {
            if (recognizer != null) recognizer.Dispose();
        }

        /// <summary>
        /// 开启语音播报
        /// </summary>
        public void StartspeechSynthesizer()
        {
            EndSpeechSynthesizer();
            speaker = new SpeechSynthesizer();
        }

        /// <summary>
        /// 结束语音播报
        /// </summary>
        public void EndSpeechSynthesizer()
        {
            if (speaker != null) speaker.Dispose();
        }

        /// <summary>
        /// 语音播报
        /// </summary>
        /// <param name="msg"></param>
        public void Speak(string msg)
        {
            if (curPrompt != null)
                speaker?.SpeakAsyncCancel(curPrompt);
            curPrompt = speaker?.SpeakAsync(msg);
        }

        private void Recognizer_SpeechRecognitionRejected(object? sender, SpeechRecognitionRejectedEventArgs e)
        {
            SpeechRecognitionRejectedEventHandler("语音输入有误");
        }


        private void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            SpeechRecognizedEventHandler(e.Result.Text);
        }


        public void Dispose()
        {
            EndSpeechSynthesizer();
            EndVoiceRecognition();
        }
    }
}
